package co.salehi.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

import co.salehi.util.Util;

@RooJavaBean
@RooToString
@RooEntity
public class Sell {
	
    
	public Sell() {
	}

	@ManyToMany(cascade = CascadeType.DETACH)
	private Set<Battery> batteries = new HashSet<Battery>();

	@ManyToMany(cascade = CascadeType.DETACH)
	private Set<Ring> rings = new HashSet<Ring>();

	@ManyToMany(cascade = CascadeType.DETACH)
	private Set<Tyre> tyres = new HashSet<Tyre>();
	
	@ManyToMany(cascade = CascadeType.DETACH)
	private Set<LabourCharge> labourCharges = new HashSet<LabourCharge>();

	@NotNull
	private String name;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "dd-MM-yyyy HH:mm")
	private Date sellDate;

	@ManyToOne
	private Operator seller;

	public static List<Sell> findSellEntries(int firstResult, int maxResults) {
		return entityManager().createQuery("select o from Sell o order by sellDate desc ", Sell.class)
				.setFirstResult(firstResult).setMaxResults(maxResults)
				.getResultList();
	}

	public static List<Sell> findAllSells() {
		return entityManager().createQuery("select o from Sell o order by sellDate desc ", Sell.class)
				.getResultList();
	}

	public void setLabourCharges(Set<LabourCharge> labourCharges) {
		this.labourCharges = labourCharges;
	}

	public Set<LabourCharge> getLabourCharges() {
		return labourCharges;
	}

}
